<?php
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die( 'Restricted access' );


/**
 * @package		Joomla
 * @subpackage	JobBoard
 */
class JobBoardTableJA_FieldList extends JTable
{

	/** @var int Primary key */
	var $id		= null;
	/** @var int Field ID */
	var $field_id 	= null;	
	/** @var int length to be truncated */
	var $truncate_length = 0;
	/** @var bool <b>True</b>, Click link to edit full record */ 
	var $linked 			= null;
	/** @var bool Standardization */
	var $clean_format 			= null;
	/** @var bool Display field in bold */
	var $is_bold 			= null;
	/** @var bool Sort field by clicking on the title */
	var $is_sortable 			= null;
	/** @var bool No table wrap */
	var $no_wrap 			= null;
	/** @var int Ordering */
	var $ordering 			= null;
	/** @var int Administration Form Only */
	var $admin_only			= null;
    /** @var int Who can view this field in list */
    var $viewable_by            = null;
	/** @var bool */
	var $field_width 			= 0;
	/** @var bool Standardization */
	var $checked_out		= null;
	/** @var bool Standardization */
	var $checked_out_time 			= null;
	
	
	
	/**
	* @param database A database connector object
	*/
	function __construct(&$db)
	{
		parent::__construct( '#__ja_form_lists', 'id', $db );
	}
	
	/**
	 * Overloaded bind function
	 *
	 * @param array $from
	 * @return Object
	 */
	function bind($from){
		global  $mainframe;
		
	    parent::bind($from);
	    
	    $filter_table		= $mainframe->getUserStateFromRequest( JBCOMNAME .".fieldlist.filter_table",		'filter_table',		'ja_profiles',	'word' );
	    if(!$this->id && $filter_table){
	    	$query = "SELECT MAX(ordering) FROM ". $this->_tbl ." AS k"
	    		." LEFT JOIN #__ja_form_fields AS ff ON k.field_id = ff.id "
	    		." WHERE table_name = ". $this->_db->Quote($filter_table) 
	    		." GROUP BY ff.table_name";
		    $this->_db->setQuery($query);
		    $maxord = $this->_db->loadResult();
		   
		    if ($this->_db->getErrorNum())
			{
				$this->setError($this->_db->getErrorMsg());
				return false;
			}
	    	$this->ordering = $maxord + 1;
		}
		return true;
	}
	
	/**
	 * Overloaded check function
	 *
	 * @access public
	 * @return boolean
	 * @see JTable::check
	 * @since 1.5
	 */
	function check()
	{
		JFilterOutput::objectHTMLSafe($this,ENT_QUOTES,'');
		if(empty($this->field_id))
			return false;
					
		return true;
			
	}
	
	
	
	function reorder($filter_table){
		$k = $this->_tbl_key;
		$query = 'SELECT k.'.$this->_tbl_key.', k.ordering, ff.table_name'
			. ' FROM '. $this->_tbl .' AS k'
			. ' LEFT JOIN #__ja_form_fields AS ff ON k.field_id = ff.id'
			. ' WHERE ff.table_name = '. $this->_db->Quote($filter_table) .' AND k.ordering >= 0 '
			. ' ORDER BY  k.ordering'
			;
		$this->_db->setQuery( $query );
		if (!($orders = $this->_db->loadObjectList()))
		{
			$this->setError($this->_db->getErrorMsg());
			return false;
		}
		// compact the ordering numbers

		for ($i=0, $n=count( $orders ); $i < $n; $i++)
		{
			
			if ($orders[$i]->ordering >= 0)
			{
				if ($orders[$i]->ordering != $i+1)
				{
					$orders[$i]->ordering = $i+1;
					$query = 'UPDATE '.$this->_tbl
					. ' SET ordering = '. (int) $orders[$i]->ordering
					. ' WHERE '. $k .' = '. $this->_db->Quote($orders[$i]->$k)
					;
					$this->_db->setQuery( $query);
					$this->_db->query();
				}
			}
		}

		return true;
	}
	
	/**
	 * Move item up or down
	 *
	 * @param unknown_type $movement
	 * @return unknown
	 */
	function move($movement, $filter_table){
		$k = $this->_tbl_key;
//		var_dump($movement);
		if($movement > 0){
		    $query = "SELECT k.id, k.ordering FROM ".$this->_tbl." AS k"
		    	." LEFT JOIN #__ja_form_fields AS ff ON k.field_id = ff.id"
		    	." WHERE ff.table_name = '$filter_table' AND k.ordering > " . $this->ordering 
		    	. " ORDER BY k.ordering ASC";
		} else if ($movement < 0){
			$query = "SELECT k.id, k.ordering FROM ".$this->_tbl." AS k"
		    	." LEFT JOIN #__ja_form_fields AS ff ON k.field_id = ff.id"
		    	." WHERE ff.table_name = '$filter_table' AND 0 < k.ordering AND k.ordering < " . $this->ordering
				. " ORDER BY k.ordering DESC";
		}
		$this->_db->setQuery($query, 0, 1);

		$row = null;
		$row = $this->_db->loadObject();
//		var_dump($row);exit;
		if (isset($row))
		{
			$query = 'UPDATE '. $this->_tbl
			. ' SET ordering = '. (int) $row->ordering
			. ' WHERE '. $this->_tbl_key .' = '. $this->_db->Quote($this->$k)
			;
			$this->_db->setQuery( $query );
            
			if (!$this->_db->query())
			{
				$err = $this->_db->getErrorMsg();
				JError::raiseError( 500, $err );
			}

			$query = 'UPDATE '.$this->_tbl
			. ' SET ordering = '.(int) $this->ordering
			. ' WHERE '.$this->_tbl_key.' = '.$this->_db->Quote($row->$k)
			;
			$this->_db->setQuery( $query );
           
			if (!$this->_db->query())
			{
				$err = $this->_db->getErrorMsg();
				JError::raiseError( 500, $err );
			}

			$this->ordering = $row->ordering;
		}
		else
		{
			$query = 'UPDATE '. $this->_tbl
			. ' SET ordering = '.(int) $this->ordering
			. ' WHERE '. $this->_tbl_key .' = '. $this->_db->Quote($this->$k)
			;
			$this->_db->setQuery( $query );

			if (!$this->_db->query())
			{
				$err = $this->_db->getErrorMsg();
				JError::raiseError( 500, $err );
			}
		}
		return true;
	}
	
	
}